package base.view.widget

import android.content.Context
import android.os.Handler
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.PopupWindow
import base.utils.SysUtils

/**
 * Created by Administrator on 2018/4/24.
 */
class PopUpFloatingWindow {
    private lateinit var mContext: Context
    private lateinit var mSysUtils: SysUtils
    private var imageView: ImageView? = null
    //    private TextView adDetail;
    private var popupView: View? = null
    private var view: View? = null
    private var popupWindow: PopupWindow? = null
    private var orgX: Int = 0
    private var orgY:Int = 0
    private var orgRX:Int = 0
    private var orgRY:Int = 0
    private var offsetX: Int = 0
    private var offsetY:Int = 0
    internal var TAG = "PopUpFloatingWindow"
    private var popUpFloatingWindow: PopUpFloatingWindow? = null

    constructor(mContext: Context, rootView: View, double12Logo: String){
        this.mContext = mContext
        initView(rootView, double12Logo)
    }

    private fun initView(rootView: View, double12Logo: String) {
        val layoutInflater = LayoutInflater.from(mContext)
//        popupView = layoutInflater.inflate(R.layout.layout_floating_view, null)
        view = rootView
//        imageView = popupView!!.findViewById(R.id.adv) as ImageView
//        ImageUtils.displayImageWebp256V2(mContext, double12Logo, imageView)
        //        adDetail = (TextView) popupView.findViewById(R.id.tv_ad9_detail);
        popupWindow = PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        popupView!!.setOnTouchListener(object : View.OnTouchListener {
            private var isClick: Boolean = false
            internal var handler = Handler()
            internal var mLongPressed: Runnable = Runnable { dismiss() }

            override fun onTouch(v: View, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        orgX = event.x.toInt()
                        orgY = event.y.toInt()
                        orgRX = event.rawX.toInt()
                        orgRY = event.rawY.toInt()
                        isClick = true
                        handler.postDelayed(mLongPressed, 1000)
                        Log.i(TAG, "ACTION_DOWN: " + event.rawX)
                    }
                    MotionEvent.ACTION_MOVE -> {
                        offsetX = event.rawX.toInt() - orgX
                        offsetY = event.rawY.toInt() - orgY
                        Log.i(TAG, "ACTION_MOVE: " + event.rawX)
                        if (Math.abs(offsetX) > 15 || Math.abs(offsetY) > 15) {
                            popupWindow!!.update(offsetX, offsetY, -1, -1, true)
                            isClick = false
                        } else {
                            isClick = true
                        }
                        handler.removeCallbacks(mLongPressed)
                    }
                    MotionEvent.ACTION_UP -> {
                        handler.removeCallbacks(mLongPressed)
                        if (Math.abs(orgRX - event.rawX.toInt()) < 15 || Math.abs(orgRY - event.rawY.toInt()) < 15 || isClick) {
                            popupView!!.performClick()
                        }
                    }
                }
                return true
            }
        })

    }

    fun show(x: Int, y: Int) {
        if (x == 0 && y == 0) {
            popupWindow!!.showAtLocation(view, Gravity.NO_GRAVITY, SysUtils.getScreenWidth(mContext) - 230, SysUtils.getScreenHeight(mContext) / 2 + 50)
        } else {
            popupWindow!!.showAtLocation(view, Gravity.NO_GRAVITY, offsetX, offsetY)
        }
    }

    fun dismiss() {
        if (popupWindow!!.isShowing) {
            popupWindow!!.dismiss()
        }
    }

    fun getXY(): IntArray {
        val location = IntArray(2)
        location[0] = offsetX
        location[1] = offsetY
        return location
    }

    fun setXY(x: Int, y: Int) {
        offsetX = x
        offsetY = y
    }

    fun windowIsShowing(): Boolean {
        return popupWindow!!.isShowing
    }

    fun setImageViewOnClickListener(listener: View.OnClickListener) {
        popupView!!.setOnClickListener(listener)
    }

    fun setImageToView(path: String) {
        if (popupWindow!!.isShowing) {
//            ImageUtils.displayProductDetailImage(path, imageView)
        }
    }

    fun setImageToView(rid: Int) {
        if (popupWindow!!.isShowing) {
            imageView!!.setImageResource(rid)
        }
    }

    fun setAdDetailText(text: String) {
        if (popupWindow!!.isShowing) {
            //            adDetail.setVisibility(View.VISIBLE);
            //            adDetail.setText(text);
        }
    }

    fun setAdDetailTextStatus(status: Int) {
        if (popupWindow!!.isShowing) {
            //            adDetail.setVisibility(status);
        }
    }

    fun setPopupWindow(popupWindow: PopUpFloatingWindow) {
        popUpFloatingWindow = popupWindow
    }
}